package com.hr.dao;

import com.hr.model.Company;
import com.hr.model.Deed;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface DeedDAO
{

    @Results(id="deed",value = {
            @Result(id = true, property = "id", column = "dee_id"),
            @Result(property = "name", column = "dee_name"),
            @Result(property = "role", column = "dee_role"),
            @Result(property = "start", column = "dee_start"),
            @Result(property = "end", column = "dee_end"),
            @Result(property = "detail", column = "dee_detail"),

            @Result(property = "position", column = "p_id", one = @One(select = "com.hr.dao.PositionDAO.getPositionByPId")),
    })
    @Select("select * from deed where dee_id=#{deeid}")
    List<Deed> getDeedsByDeeId(@Param("deeid")int deeid);

    @ResultMap("deed")
    @Select("select * from deed where a_id=#{aid}")
    List<Deed> getDeedsByAId(@Param("aid")int aid);

    @SelectKey(keyProperty = "deed.id",before = false,resultType = Integer.class,statement = "select last_insert_id()")
    @Insert("insert into deed (p_id, a_id, dee_name, dee_role, dee_start, dee_end, dee_detail) " +
            "values (#{deed.position.id}, #{aid},#{deed.name},#{deed.role},#{deed.start},#{deed.end},#{deed.detail})")
    void insertDeedByAId(@Param("aid")int aid,@Param("deed")Deed deed);

    @Update("update deed set p_id=#{deed.position.id}, dee_name=#{deed.name}, dee_role=#{deed.role}, dee_start=#{deed.start}, dee_end=#{deed.end}, dee_detail=#{deed.detail} " +
            "where dee_id=#{deed.id} and a_id=#{aid}")//a_id不一致的不更改
    void updateDeed(@Param("aid")int aid,@Param("deed")Deed deed);

    @Update("delete from deed where dee_id=#{deeid}")//a_id不一致的不更改
    void deleteDeed(@Param("deeid")int deeid);
}
